#!/bin/bash
#pMusic - external menu
#GPL - Copyright Sigmund Berglund

[ $PTR_BTN != 3 ] && exit
export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
SF='space-expand="false" space-fill="false"'
ST='space-expand="true" space-fill="true"'

template(){
	[[ "$4" = "require_file" && "$PLAYLIST" = @(*cdda:*|*http*) ]] && SENSITIVE='<sensitive>false</sensitive>' #in case cdda or stream, disable some choices and some new
	[[ "$5" = "require_editor" && ! "$AUDIO_EDITOR" ]] && SENSITIVE='<sensitive>false</sensitive>'

	echo '
	<eventbox above-child="true" visible-window="true">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$TOOLTIP' '$ST'>
	   '$SENSITIVE'
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="enter-notify-event">hide:'$2'</action>
	 <action signal="enter-notify-event">show:'$2'B</action>
	 <variable>'$2'</variable>
	</eventbox>
	<eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$TOOLTIP' '$ST'>
	   '$SENSITIVE'
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="leave-notify-event">show:'$2'</action>
	 <action signal="leave-notify-event">hide:'$2'B</action>
	 <action signal="button-release-event">EXIT:'$2'</action>
	 <variable>'$2'B</variable>
	</eventbox>
	'
}

#define favorites menuitem
MENUITEMS="`find "$STORAGE_DIR/favorite_lists/" -type f -printf "<menuitem><label>\\\"%f\\\"</label><action>echo \\\"%f\\\" > $WORKDIR/tmp_favorite_list</action><action>refresh:FAVORITE_LIST</action></menuitem>\n" | sed 's%.pmd%%g'`"
[ ! "$MENUITEMS" ] && MENUITEMS="<menuitem><label>".."</label></menuitem>"
gettext 'My favorite list' > $WORKDIR/tmp_favorite_list #default value
	
export gtkdialog_menu='
<window decorated="false" border-width="0" skip_taskbar_hint="true" resizable="false" modal="true" '$ST'>
 <eventbox name="rightclick_menu_frame" above-child="false">
  <vbox border-width="1">
   <eventbox>
    <vbox spacing="0" border-width="7">
     '"

     $(template " $(gettext 'Play')" play icon_play.svg)
     $(template " $(gettext 'Remove from playQueue (middle-button)')" remove icon_pmusic_remove.svg)
     <hseparator height-request=\"5\"></hseparator>
     $(template " $(gettext 'Track info')" info icon_info.svg)"'

     <eventbox above-child="true" visible-window="true">
      <hbox spacing="7" border-width="3">
       <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/icon_favorites_add.svg</input></pixmap>
       <text xalign="0" '$ST'>
        <label>'$(gettext 'Add to Favorites')'</label>
       </text>
      </hbox>
      <action signal="enter-notify-event">hide:FAV_ADD</action>
      <action signal="enter-notify-event">show:FAV_ADDB</action>
      <variable>FAV_ADD</variable>
     </eventbox>
     <eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
      <hbox spacing="7" border-width="3">
       <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/icon_favorites_add.svg</input></pixmap>
       <text xalign="0" '$ST'>
        <label>'$(gettext 'Add to Favorites')'</label>
       </text>
      </hbox>
      <action signal="leave-notify-event">show:FAV_ADD</action>
      <action signal="leave-notify-event">hide:FAV_ADDB</action>
      <action signal="button-release-event">show:HBOX_FAV</action>
      <variable>FAV_ADDB</variable>
     </eventbox>

     <hbox visible="false" spacing="2" tooltip-text="'$(gettext 'What favorite list should the item be added to?
An empty field adds a favorite Mark')'">
      <text width-request="25"><label>""</label></text>
      <entry secondary-icon-stock="gtk-clear">
       <variable>FAVORITE_LIST</variable>
       <input file>'$WORKDIR'/tmp_favorite_list</input>
       <output file>'$WORKDIR'/tmp_favorite_list</output>
       <action signal="secondary-icon-release">clear:FAVORITE_LIST</action>
       <action signal="activate">save:FAVORITE_LIST</action>
       <action signal="activate">EXIT:add_favorites</action>
      </entry>
      <menubar>
       <menu>
       '"$MENUITEMS"'
       <label>..</label>
       </menu>
      </menubar>
      <button stock-icon-size="1">
       <input file stock="gtk-favorites_add"></input>
       <height>16</height>
       <action>save:FAVORITE_LIST</action>
       <action>EXIT:add_favorites</action>
      </button>
      <variable>HBOX_FAV</variable>
     </hbox>'"

     $(template " $(gettext 'Show location')" show_location icon_directory.svg require_file)
     $(template " $(gettext 'Edit audio-stream (and keep meta-info)')" edit_audio icon_edit_audio.svg require_file require_editor)
     $(template " $(gettext 'Send to export')" send_to_export icon_pmusic_redo.svg)
     $(template " $(gettext 'Send to masstagger')" masstagger icon_pmusic_redo.svg require_file)
     <hseparator height-request=\"5\"></hseparator>
     $(template " $(gettext 'Rename source file')" rename icon_rename.svg require_file)
     $(template " $(gettext 'Copy source file')" copy icon_copy.svg require_file)
     $(template " $(gettext 'Delete source file')" delete icon_delete.svg require_file)
     "'
    </vbox>
   </eventbox>
  </vbox>
 </eventbox>
 <action signal="focus-out-event">EXIT:exit</action>
</window>'

. $APPDIR/func -set_gtk menu #set GTK theme - unique gtk-theme for menu

I=$IFS; IFS=""
for STATEMENTS in $($GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-300))"+"$(($PTR_Y_ROOT-5))"); do
	eval $STATEMENTS
done
IFS=$I

export PTR_BTN=""
case "$EXIT" in

play)
	echo 0 > $WORKDIR/SS
	$APPDIR/func_player -stop -skip_update
	$APPDIR/func_player -playing "$(grep -Fm1 "$PLAYLIST" $WORKDIR/playlist)" &
	sleep 0.2
	[ -f $WORKDIR/gui_is_running ] && echo true | tee $WORKDIR/UPDATE_GUI $WORKDIR/UPDATE_PLAYLIST
	;;

send_to_export)
	TMP="`LC_ALL=C ps -eo pid,command`"
	case $TMP in
		*pmusic*func_export*) echo;;
		*) $APPDIR/func_export & ;;
	esac
	echo ${PLAYLIST#*\ } >> $WORKDIR/exportlist
	;;

masstagger)
	TMP="`LC_ALL=C ps -eo pid,command`"
	case $TMP in
		*pmusic*func_id3tagger*) echo;;
		*) $APPDIR/func_id3tagger &	;;
	esac
	echo ${PLAYLIST#*\ } >> $WORKDIR/id3tagger
	;;

add_favorites)
	gettext 'Add to Favorites' > $WORKDIR/splashtext
	$APPDIR/box_splash &
	#---
	[ "$(<$WORKDIR/tmp_favorite_list)" ] && LIST="$(<$WORKDIR/tmp_favorite_list).pmd"
	TMP="`cut -d' ' -f2- <<< "$PLAYLIST"`"
	$APPDIR/func_favorites -add_favorites "$TMP" "$LIST"
	#---
	sleep 0.5
	echo 100 > $WORKDIR/splash
	;;

edit_audio)
	#store meta-info because editor will most likely destroy them
	. $APPDIR/func_id3io "${PLAYLIST#*\ }" -read
	OLD_LENGTH="$LENGTH"
	#Run editor
	export GTK2_RC_FILES="$HOME/.gtkrc-2.0" #use global gtk-theme for editor
	$AUDIO_EDITOR "${PLAYLIST#*\ }"
	#save meta-info to new (edited) file
	. $APPDIR/func_id3io "${PLAYLIST#*\ }" -write
	#update length in db
	ffmpeg -i "${PLAYLIST#*\ }" > $WORKDIR/tmp_id3ffmpeg 2>&1
	LENGTH="`grep -wm1 "Duration:" $WORKDIR/tmp_id3ffmpeg | cut -d. -f1 | awk -F':' '{print $3":"$4}'`"
	( flock -e 200
		grep -aF "${PLAYLIST#*\ }" "$STORAGE_DIR/index_mymusic" | awk -F'|' -v VAR1="$LENGTH" '{print $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6 "|" $7 "|" $8 "|" $9 "|" $10 "|" $11 "|" VAR1 "|" $13 "|" $14 "|" $15 "|" $16 "|" $17 "|" $18 "|" $19 "|" $20 "|" $21}' > $WORKDIR/tmp_edit_audio
		grep -aFv "${PLAYLIST#*\ }" "$STORAGE_DIR/index_mymusic" >> $WORKDIR/tmp_edit_audio
		mv $WORKDIR/tmp_edit_audio "$STORAGE_DIR/index_mymusic"
	) 200>$WORKDIR/mymusic_db.lockfile
	#update playlist
	( flock -e 201
		sed -i "s%$OLD_LENGTH%$LENGTH%g" $WORKDIR/playlist
	) 201>$WORKDIR/playlist.lockfile
	echo true | tee $WORKDIR/UPDATE_GUI $WORKDIR/UPDATE_PLAYLIST
	;;

remove)
	$APPDIR/func -remove_from_list
	cp $WORKDIR/playlist $WORKDIR/playlist_backup #update for the gtkdialog-function in playqueue - see gui_playlist 
	echo true | tee $WORKDIR/UPDATE_GUI $WORKDIR/UPDATE_PLAYLIST
	;;

info)			$APPDIR/func_trackinfo "${PLAYLIST#*\ }" -norefresh & ;;
show_location)	$APPDIR/func -show_location "${PLAYLIST#*\ }" & ;; #remove numbering
rename)			$APPDIR/func -rename "${PLAYLIST#*\ }";;
copy)			$APPDIR/func -copy "${PLAYLIST#*\ }";;
delete)			$APPDIR/func -delete "${PLAYLIST#*\ }";;

esac
